package com.ruoyi.project.channel.businessCashChange.controller;

import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.channel.businessCashChange.domain.BusinessCashChange;
import com.ruoyi.project.channel.businessCashChange.service.IBusinessCashChangeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 资金变动信息操作处理
 *
 * @author zero
 * @date 2019-05-18
 */
@Controller
@RequestMapping("/channel/businessCashChange")
public class BusinessCashChangeController extends BaseController {
    private String prefix = "channel/businessCashChange";

    @Autowired
    private IBusinessCashChangeService businessCashChangeService;

    @RequiresPermissions("channel:businessCashChange:view")
    @GetMapping()
    public String businessCashChange() {
        return prefix + "/businessCashChange";
    }

    /**
     * 查询资金变动列表
     */
    @RequiresPermissions("channel:businessCashChange:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusinessCashChange businessCashChange) {
        startPage();
        String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
        if (roleKey.equals("mall")) {
            businessCashChange.setBusinessId(ShiroUtils.getUserId());
        } else if (roleKey.equals("agent")) {
            businessCashChange.getParams().put("parentUserId", ShiroUtils.getUserId());
        } else if (roleKey.equals("channel")) {
            businessCashChange.getParams().put("channelId", ShiroUtils.getUserId());
        } else if (roleKey.equals("salesman")) {
            businessCashChange.getParams().put("salesmanId", ShiroUtils.getUserId());
        } else if (roleKey.equals("group")) {
            businessCashChange.getParams().put("groupUserId", ShiroUtils.getUserId());
        }
        List<BusinessCashChange> list = businessCashChangeService.selectBusinessCashChangeList(businessCashChange);
        return getDataTable(list);
    }

    /**
     * 查询佣金列表
     */
    @PostMapping("/list/self")
    @ResponseBody
    public TableDataInfo listSelf(BusinessCashChange businessCashChange) {
        businessCashChange.setBusinessId(ShiroUtils.getUserId());
        startPage();
        List<BusinessCashChange> list = businessCashChangeService.selectBusinessCashChangeList(businessCashChange);
        return getDataTable(list);
    }


    /**
     * 导出资金变动列表
     */
    @RequiresPermissions("channel:businessCashChange:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusinessCashChange businessCashChange) {
        String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
        if (roleKey.equals("mall")) {
            businessCashChange.setBusinessId(ShiroUtils.getUserId());
        } else if (roleKey.equals("agent")) {
            businessCashChange.getParams().put("parentUserId", ShiroUtils.getUserId());
        }
        List<BusinessCashChange> list = businessCashChangeService.selectBusinessCashChangeList(businessCashChange);
        ExcelUtil<BusinessCashChange> util = new ExcelUtil<BusinessCashChange>(BusinessCashChange.class);
        return util.exportExcel(list, "businessCashChange");
    }

    /**
     * 新增资金变动
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存资金变动
     */
    @RequiresPermissions("channel:businessCashChange:add")
    @Log(title = "资金变动", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusinessCashChange businessCashChange) {
        return toAjax(businessCashChangeService.insertBusinessCashChange(businessCashChange));
    }

    /**
     * 修改资金变动
     */
    @GetMapping("/edit/{cashId}")
    public String edit(@PathVariable("cashId") Long cashId, ModelMap mmap) {
        BusinessCashChange businessCashChange = businessCashChangeService.selectBusinessCashChangeById(cashId);
        mmap.put("businessCashChange", businessCashChange);
        return prefix + "/edit";
    }

    /**
     * 修改保存资金变动
     */
    @RequiresPermissions("channel:businessCashChange:edit")
    @Log(title = "资金变动", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusinessCashChange businessCashChange) {
        return toAjax(businessCashChangeService.updateBusinessCashChange(businessCashChange));
    }

    /**
     * 删除资金变动
     */
    @RequiresPermissions("channel:businessCashChange:remove")
    @Log(title = "资金变动", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(businessCashChangeService.deleteBusinessCashChangeByIds(ids));
    }

}
